<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/5 15:36
 */

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\Models\SymptomDictionary;
use App\Project\Traits\PinYinSearchAble;
use Illuminate\Http\Request;

/**
 * 症状列表获取
 * Class SymptomController
 * @package App\Http\Controllers\Manage
 */
class SymptomController extends ApiController
{
    use PinYinSearchAble;

    /**
     * @var array
     */
    protected $searchQueryMap = [
        'name' => ['field' => 'name', 'operator' => 'like'],
    ];

    /**
     * @var \App\Models\SymptomDictionary
     */
    protected $dictionary;

    /**
     * @var \Illuminate\Http\Request
     */
    protected $request;

    /**
     * SymptomController constructor.
     * @param \App\Models\SymptomDictionary $dictionary
     * @param \Illuminate\Http\Request $request
     */
    public function __construct(SymptomDictionary $dictionary, Request $request)
    {
        parent::__construct();
        $this->dictionary = $dictionary;
        $this->request = $request;
    }

    public function index()
    {
        $this->pinYinSearch($this->request->input(), $this->dictionary);

        $symptoms = $this->chinese();

        if (\Auth::user()->hospital_id) {

            if (\Auth::user()->hospital->isWestern()) {
                $symptoms = $this->western();
            }

        }
        $data['sysmpots'] =  array_values($symptoms->toArray());
        $data['thyroidData'] = $this->thyroidData()->toArray();

//        return $this->response(array_values($symptoms->toArray()));
        return $this->response($data);
    }

    /**
     * 主诉症状的模糊查询搜索数据
     * @author yangjianqiang
     * @date    20190318
     * @return \Illuminate\Http\JsonResponse
     *
     */
    public function lists()
    {
        $name = trim($this->request->input('name'));
        $data = [];
        if (\Auth::user()->hospital_id) {
            //西医类型
            $types = app('field')->get('symptom.type');
            $typeid = 'typeid';
            if (\Auth::user()->hospital->isCn()) {
                //中医类型
                $typeid = 'cn_typeid';
                $types = app('field')->get('symptom.cn_type');
            }
            //获取主诉的需要的类型的类型标识id
            $types = array_keys($types);
            $symptoms = $this->dictionary->whereNotNull($typeid)
                ->where('dic_type', '=',1)
                ->whereIn($typeid,$types)
                ->where(function($query) use ($name) {
                    $query->where('name','like','%'.$name.'%')
                        ->orWhere('pinyin','like','%'.$name.'%')
                        ->orWhere('intact_pinyin','like','%'.$name.'%');
                })->get()->toArray();
            $thyroidData = $this->dictionary->whereNull($typeid)
                ->where('dic_type','=',2)
                ->where(function($query) use ($name) {
                    $query->where('name','like','%'.$name.'%')
                        ->orWhere('pinyin','like','%'.$name.'%')
                        ->orWhere('intact_pinyin','like','%'.$name.'%');
                })->get()->toArray();
            $data['sysmpots'] =  $symptoms;
            $data['thyroidData'] = $thyroidData;
        }
        return $this->response($data);

    }

    protected function chinese()
    {
        $types = app('field')->get('symptom.cn_type');

        $symptoms = $this->dictionary->whereNotNull('cn_typeid')->where('dic_type',1)->get()->groupBy('cn_typeid');

        return $symptoms->map(function ($symptom, $typeId) use ($types) {
            if (isset($types[$typeId])) {
                return ['data' => $symptom->toArray(), 'title' => $types[$typeId]];
            }
        })->reject(function ($symptom) {
            return is_null($symptom);
        });
    }

    protected function western()
    {
        $types = app('field')->get('symptom.type');
        return $this->dictionary->whereNotNull('typeid')->where('dic_type',1)->get()->sortBy('typeid')->groupBy('typeid')
            ->map(function ($symptoms, $typeId) use ($types) {
                return ['data' => $symptoms, 'title' => $types[$typeId]];
            });
    }

    /**
     * 甲状腺数据
     */
    public function thyroidData(){

        return $this->dictionary->where('dic_type','2')->get();
    }
}